/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import org.threeten.bp.format.DateTimeFormatter;

/**
 * @since 2021-07-12
 */
public class DialogsAbility extends BaseAbility implements Component.ClickedListener {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");
    private Button button_normal_dialog;
    private Button button_simple_dialog;
    private CommonDialog normaldialog;
    private CommonDialog calendardialog;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarTitle.setText("Calendar in Dialogs");
        button_normal_dialog = (Button) findComponentById(ResourceTable.Id_button_normal_dialog);
        button_normal_dialog.setClickedListener(this);
        button_simple_dialog = (Button) findComponentById(ResourceTable.Id_button_simple_dialog);
        button_simple_dialog.setClickedListener(this);
        initnormaldialog();
        initcalendardialog();
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_dialogs;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_normal_dialog:
                normaldialog.show();
                break;
            case ResourceTable.Id_button_simple_dialog:
                calendardialog.show();
                break;
            default:
                throw new IllegalStateException("Unexpected value: ");
        }
    }

    private void initnormaldialog() {
        normaldialog = new CommonDialog(this);
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_normal, null, false);
        normaldialog.setAutoClosable(false);
        normaldialog.setContentCustomComponent(mComponent);
        normaldialog.setTransparent(true);

        DirectionalLayout mDirectionalLayout = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        DirectionalLayout mdl = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_dl_bg);
        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        mDirectionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                normaldialog.hide();
            }
        });
        mdl.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {

            }
        });
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                normaldialog.hide();
            }
        });
    }

    private void initcalendardialog() {
        calendardialog = new CommonDialog(this);
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_calendarview, null, false);
        calendardialog.setAutoClosable(false);
        calendardialog.setContentCustomComponent(mComponent);
        calendardialog.setTransparent(true);
        DirectionalLayout mDirectionalLayout = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        DirectionalLayout mdl = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_dl_bg);
        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        Text tv_date = (Text) mComponent.findComponentById(ResourceTable.Id_tv_date);
        MaterialCalendarView widget = (MaterialCalendarView) mComponent.findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
                final String text = selected ? FORMATTER.format(date.getDate()) : "No Selection";
                tv_date.setText(text);
            }
        });

        mDirectionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                calendardialog.hide();
            }
        });
        mdl.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {

            }
        });
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                calendardialog.hide();
            }
        });

    }
}
